import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/* Router Modules */
import componentsRouter from './modules/components'
import chartsRouter from './modules/charts'
import tableRouter from './modules/table'
import nestedRouter from './modules/nested'
import exampleRouter from '@/router/modules/example'
import tabRouter from '@/router/modules/tab'
import excelRouter from '@/router/modules/excel'
import errorLogRouter from '@/router/modules/errorlog'
import zipRouter from '@/router/modules/zip'
import miscRouter from '@/router/modules/misc'

/**
 * Note: 子菜单仅仅在route的children数量大于等于1时显示。
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   如果设置为true, 该项不会出现在左栏(默认为false，即默认展示)
 * alwaysShow: true               如果设置为true, 总是显示根菜单
 *                                如果没有设置alwaysShow, 当项目有1个以上的子路由时，它会变成嵌套模式，否则不展示根菜单。
 * redirect: noRedirect           如果设置为noRedirect, 面包屑中不会重定向。
 * name:'router-name'             名字用于<keep-alive>(必须设置!!!)
 * meta : {
    roles: ['admin','editor']    控制页面哪些角色可看 (这里可以设置多个角色)
    title: 'title'               在侧边栏和面包屑中展示的名称(推荐设置)
    icon: 'svg-name'/'el-icon-x' 在侧边栏上显示的图标类
    noCache: true                如果设置为true,页面不会被缓存。默认是false, 即页面会缓存。
    affix: true                  如果设置为true, 标签会粘在tags-view中。
    breadcrumb: false            如果设置为false, 该项会在面包屑中隐藏。默认为true,即会在面包屑中展示。
    activeMenu: '/example/list'  如果设置路径，侧边栏会高亮你设置的路径，即当前菜单。
  }
 */

/**
 * constantRoutes
 * 这是一些基础页面，不需要权限。所有角色都可访问。
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: 'Dashboard', icon: 'dashboard', affix: true }
      }
    ]
  },
  {
    path: '/documentation',
    component: Layout,
    children: [
      {
        path: 'index',
        component: () => import('@/views/documentation/index'),
        name: 'Documentation',
        meta: { title: 'Documentation', icon: 'documentation', affix: true }
      }
    ]
  },
  {
    path: '/guide',
    component: Layout,
    redirect: '/guide/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/guide/index'),
        name: 'Guide',
        meta: { title: 'Guide', icon: 'guide', noCache: true }
      }
    ]
  },
  {
    path: '/profile',
    component: Layout,
    redirect: '/profile/index',
    hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/profile/index'),
        name: 'Profile',
        meta: { title: 'Profile', icon: 'user', noCache: true }
      }
    ]
  }
]

/**
 * asyncRoutes
 * 这些路由需要动态根据用户角色进行加载。
 */
export const asyncRoutes = [
  {
    path: '/permission',
    component: Layout,
    redirect: '/permission/page',
    alwaysShow: true, // will always show the root menu
    name: 'Permission',
    meta: {
      title: 'Permission',
      icon: 'lock',
      roles: ['admin', 'editor'] // you can set roles in root nav
    },
    children: [
      {
        path: 'page',
        component: () => import('@/views/permission/page'),
        name: 'PagePermission',
        meta: {
          title: 'Page Permission',
          roles: ['admin'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'directive',
        component: () => import('@/views/permission/directive'),
        name: 'DirectivePermission',
        meta: {
          title: 'Directive Permission'
          // if do not set roles, means: this page does not require permission
        }
      },
      {
        path: 'role',
        component: () => import('@/views/permission/role'),
        name: 'RolePermission',
        meta: {
          title: 'Role Permission',
          roles: ['admin']
        }
      }
    ]
  },

  {
    path: '/icon',
    component: Layout,
    children: [
      {
        path: 'index',
        component: () => import('@/views/icons/index'),
        name: 'Icons',
        meta: { title: 'Icons', icon: 'icon', noCache: true }
      }
    ]
  },

  /**
   *  路由映射比较长，比较大的时候，可以考虑按照不同的模块拆分成多个小的模块。
   */
  componentsRouter,
  chartsRouter,
  nestedRouter,
  tableRouter,
  exampleRouter,
  tabRouter,
  excelRouter,
  errorLogRouter,
  zipRouter,
  miscRouter,

  // 404页面必须在最后!!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
